<?php

namespace app\admin\model;

use think\Model;


class QuestionData extends Model
{

    

    

    // 表名
    protected $name = 'child_q_data';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'delete_time_text',
        'update_time_text'
    ];
    

    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    //获取同一问卷模板下列表（获取全部，不分页）
    public static function getRealQuestionnaireDoneList($qid , $order = null, $order1 = null)
    {
        $res = self::with('withQFormat')
        ->where('qid','=',$qid);
        $orderStr =self::getOrderStr($order,$order1);
        return $res->order($orderStr)->select()
            ->append(['user_name'])
            ->visible(['with_q_format', 'id', 'mark', 'note', 'dealt', 'viewed', 'pass', 'create_time','is_automatic','qid']);
    }

    public function withQFormat()
    {
        return $this->belongsTo('QuestionFormat', 'id', 'qid')->field(['id', 'name', 'q_class_id', 'show_mode', "full_mark", "pass_mark", "is_automatic",'create_time']);
    }
    //处理排序字符串
    private static function getOrderStr($order = null, $order1 = null){
        $orderStr="create_time desc";
        if ($order) {
            switch ($order) {
                case 1:
                    $orderStr='create_time desc';
                    break;
                case 2:
                    $orderStr='create_time asc';
                    break;
                case 3:
                    $orderStr='qid asc';
                    break;
                case 4:
                    $orderStr='pass asc';
                    break;
                case 5:
                    $orderStr='is_automatic desc';
                    break;
                case 6:
                    $orderStr='is_automatic asc';
                    break;
                case 7:
                    $orderStr='dealt asc';
                    break;
                case 8:
                    $orderStr='dealt desc';
                    break;
            }
        }
        if ($order1) {
            if($order){
                $orderStr =$orderStr.', ';
            }else{
                $orderStr = '';
            }
            switch ($order1) {
                case 1:
                    $orderStr=$orderStr.'create_time desc ';
                    break;
                case 2:
                    $orderStr=$orderStr.'create_time asc';
                    break;
                case 3:
                    $orderStr=$orderStr.'qid asc';
                    break;
                case 4:
                    $orderStr=$orderStr.'pass asc';
                    break;
                case 5:
                    $orderStr=$orderStr.'is_automatic desc';
                    break;
                case 6:
                    $orderStr=$orderStr.'is_automatic asc';
                    break;
                case 7:
                    $orderStr=$orderStr.'dealt asc';
                    break;
                case 8:
                    $orderStr=$orderStr.'dealt desc';
                    break;
            }
        }
        return $orderStr;
    }

    //获取问卷数据详情
    public static function getQuestionnaireDataDetail($id)
    {
        $res = self::get($id);
        if ($res) {
            $res = $res->visible(['qid', 'uid', 'id', 'mark', 'note', 'dealt', 'viewed', 'data']);
        } 
        return $res;
    }

}
